#pragma once
#include <iostream>
#include <string>
#include "protocol.hpp"
using namespace std;

namespace CAL {
    enum {
        Success = 0,
        DivZero,
        ModZero,
        Unknow
    };
    class Calculate {
    public :
        Calculate() {

        }
        shared_ptr<Protocol::Response> Cal(shared_ptr<Protocol::Request> req) {
            shared_ptr<Protocol::Response> resp = fact.BuildResponse();
            resp->SetCode(Success);
            switch(req->GetOp()) {
                case '+' :
                    resp->SetResult(req->GetX() + req->GetY());
                    break;
                case '-' :
                    resp->SetResult(req->GetX() - req->GetY());
                    break;
                case '*' :
                    resp->SetResult(req->GetX() * req->GetY());
                    break;
                case '/' :
                    {
                        if (req->GetY() == 0) {
                            resp->SetCode(DivZero);
                        } else {
                            resp->SetResult(req->GetX() / req->GetY());
                        }
                        break;
                    }
                case '%' :
                    {
                        if (req->GetY() == 0) {
                            resp->SetCode(ModZero);
                        } else {
                            resp->SetResult(req->GetX() % req->GetY());
                        }
                        break;
                    }
                default :
                    resp->SetCode(Unknow);
                    break;
            }
            return resp;
        }
        ~Calculate() {

        }

    private :
        Protocol::Factory fact;
    };
};
